package common.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2025-02-25
 */
public class WebUtils {

    public static String getRemoteIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP"); // Nginx常用
        if (StringUtils.isBlank(ip)) {
            ip = request.getHeader("X-Forwarded-For");
            if (ip != null && ip.contains(",")) {
                ip = ip.split(",")[0].trim();
            }
        }
        if (StringUtils.isBlank(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
